package com.ikas.ai.server.module.morningpaper.util;

import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.util.Date;

/**
 * @author li.hexian
 * 早报相关时间方法集合
 */
@Component
public class MorningPaperTimeUtil {
    @Value("${morning-paper.statistics-start-time.hours:08}")
    Integer statisticsStartTimeHours;
    @Value("${morning-paper.statistics-start-time.min:00}")
    Integer statisticsStartTimeMin;
    @Value("${morning-paper.statistics-end-time.hours:08}")
    Integer statisticsEndTimeHours;
    @Value("${morning-paper.statistics-end-time.min:00}")
    Integer statisticsEndTimeMin;
    @Value("${morning-paper.date-format:yyyy年MM月dd日}")
    String dateTimeFormat;
    @Value("${morning-paper.date-format.without-year:MM月dd日}")
    String dateTimeFormatWithoutYear;
    @Value("${morning-paper.time-format:HH:mm}")
    String timeTimeFormat;

    private final static String TIME_ZONE_CN = "Asia/Shanghai";

    /**
     * 获取当日时间，仅包含日期，用作Variable表Date列存储
     *
     * @return
     */
    public Date getYesterdayDayDate() {
        return DateUtil.offsetDay(DateTime.now(), -1).toSqlDate();
    }


    /**
     * 获取统计开始时间
     */
    public Date getStatisticsStartTime() {
        return combineDateAndTime(
                getCorrectDayLocalDate().minusDays(1),
                LocalTime.of(statisticsStartTimeHours, statisticsStartTimeMin)
        );
    }

    /**
     * 获取早报统计开始时间
     */
    public Date getPaperStatisticsStartTime() {
        DateTime yesterday = DateUtil.yesterday();
        yesterday.setField(DateField.HOUR_OF_DAY, statisticsStartTimeHours);
        yesterday.setField(DateField.MINUTE, statisticsStartTimeMin);
        yesterday.setField(DateField.SECOND, 0);
        yesterday.setField(DateField.MILLISECOND, 0);
        return yesterday;
    }

    /**
     * 获取统计结束时间
     */
    public Date getStatisticsEndTime() {
        return combineDateAndTime(
                getCorrectDayLocalDate(),
                LocalTime.of(statisticsEndTimeHours, statisticsEndTimeMin)
        );
    }

    /**
     * 获取早报统计结束时间
     */
    public Date getPaperStatisticsEndTime() {
        DateTime now = DateTime.now();
        now.setField(DateField.HOUR_OF_DAY, statisticsEndTimeHours);
        now.setField(DateField.MINUTE, statisticsEndTimeMin);
        now.setField(DateField.SECOND, 0);
        now.setField(DateField.MILLISECOND, 0);
        return now;
    }

    /**
     * 获取仅包含月和日的中文起始日期和结束日期
     *
     * @return
     */
    public String getStartDateText() {
        return new SimpleDateFormat(dateTimeFormatWithoutYear).format(Date.from(getCorrectDayLocalDate().minusDays(1).atStartOfDay().toInstant(ZoneId.of(TIME_ZONE_CN).getRules().getOffset(getCorrectDayLocalDate().minusDays(1).atStartOfDay()))));
    }

    public String getEndDateText() {
        return new SimpleDateFormat(dateTimeFormatWithoutYear).format(Date.from(getCorrectDayLocalDate().atStartOfDay().toInstant(ZoneId.of(TIME_ZONE_CN).getRules().getOffset(getCorrectDayLocalDate().atStartOfDay()))));
    }

    public String getTimeTimeFormat() {
        return timeTimeFormat;
    }

    public String getDateTimeFormat() {
        return dateTimeFormat;
    }

    public Integer getStatisticsStartTimeMin() {
        return statisticsStartTimeMin;
    }

    public Integer getStatisticsStartTimeHours() {
        return statisticsStartTimeHours;
    }

    public Integer getStatisticsEndTimeMin() {
        return statisticsEndTimeMin;
    }

    public Integer getStatisticsEndTimeHours() {
        return statisticsEndTimeHours;
    }


    private LocalDate getCorrectDayLocalDate() {
        LocalDateTime currentDateTime = LocalDateTime.now();
        LocalTime comparisonTime = LocalTime.of(statisticsEndTimeHours, statisticsEndTimeMin);
        LocalDate resultDate;
        if (currentDateTime.toLocalTime().isBefore(comparisonTime)) {
            resultDate = currentDateTime.minusDays(1).toLocalDate();
        } else {
            resultDate = currentDateTime.toLocalDate();
        }
        return resultDate;
    }

    private static Date combineDateAndTime(LocalDate date, LocalTime time) {
        return Date.from(
                LocalDateTime.of(
                        date, time
                ).atZone(ZoneId.of(TIME_ZONE_CN)).toInstant()
        );
    }
}
